//
//  HHDatePickerView.h
//  Pods
//
//  Created by chun.chen on 2018/6/13.
//  日期选择期picker

#import "HHDatePickerView.h"
#import <BCUIKit/UIView+BCHelper.h>
#import <BCUIKit/UIViewController+HHPage.h>
#import <BCFoundation/BCFoundationUtils.h>
#import <BCComConfigKit/BCComConfigKit.h>
#import "BCFoundation.h"
#import "Masonry.h"

#define kHHDatePickerView_PickH     200
#define kHHDatePickerView_PickBarH  44


@interface HHDatePickerView ()
@property (nonatomic, strong, readwrite) UIDatePicker *datePicker;
@property (nonatomic, strong) UIView *courentView;
@property (nonatomic, strong) UIView *splitLb;
@property (nonatomic, strong, readwrite) UIButton *doneButton;
@property (nonatomic, strong, readwrite) UIButton *cancelButton;
@end

@implementation HHDatePickerView
//MARK: - system
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        self.alpha = 0;
    }
    return self;
}
- (void)dealloc
{
    BCLogDealloc();
}

+ (instancetype)showDatePicker:(UIView*)aView date:(nullable NSDate *)date minDate:(nullable NSDate *)minDate maxDate:(nullable NSDate *)maxDate complete:(void(^ _Nullable)(NSDate * date) )complete {
    HHDatePickerView *pickerView = [[HHDatePickerView alloc] init];
    if(date){
        pickerView.currentDate = date;
    }
    if(minDate){
        pickerView.datePicker.minimumDate = minDate;
    }
    if(maxDate){
        pickerView.datePicker.maximumDate = maxDate;
    }
    pickerView.completeBlock = complete;
    if(aView) {
        aView = [UIViewController bc_topPage].view;
    }
     [pickerView showPicker:aView];
    return pickerView;  // ✅ 修复关键

}

///MARK: - setter
- (void)setCurrentDate:(NSDate *)currentDate {
    _currentDate = currentDate;
    if(self.superview){
        [self.datePicker setDate:currentDate];
    }

}

//MARK: - Public Method
- (void)showPicker:(UIView *)superView {
    if (!superView) {
        return;
    }
    self.frame = superView.bounds;
    [self configSubViews];
    [superView addSubview:self];
    [self showAnimtion];

    if(self.currentDate){
        [self.datePicker setDate:self.currentDate];
    }

}
- (void)hidePicker {
    [self hideAnimation];
}

//MARK: - Private Method
- (void)showAnimtion {
    if (self.alpha !=0) {
        return;
    }
    self.hidden = NO;
    [self.courentView setBc_y:self.bc_height];
    [UIView animateWithDuration:0.25f animations:^{
        [self.courentView setBc_y:self.bc_height-self.courentView.bc_height];
        self.alpha = 1;
    }];
}
- (void)hideAnimation {
    if (self.alpha !=1) {
        return;
    }
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    self.hidden = NO;
    [UIView animateWithDuration:0.25f animations:^{
        [self.courentView setBc_y:self.bc_height];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.hidden = YES;
    }];
}

//MARK: - Event Response
- (void)cancelBarItemAction:(id)sender {
    [self hideAnimation];
}
- (void)doneBarItemAction:(id)sender {
    NSDate *selectDate = self.datePicker.date;
    if (self.completeBlock) {
        self.completeBlock(selectDate);
    }
    [self hideAnimation];
}

- (void)datePickerValueChanged:(id)sender {
    
}

//MARK: - Subviews
- (void)configSubViews {
    if (_courentView.superview) {
        return;
    }
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3f];
    self.hidden = YES;
    self.alpha = 0;
    @BCWeakify(self);
    [self bc_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @BCStrongify(self);
        CGPoint touchPoint = [gestureRecoginzer locationInView:self];
        if (CGRectContainsPoint(self.courentView.frame, touchPoint)) {
            return ;
        }
        [self hideAnimation];
    }];
    [self addSubview:self.courentView];
    [self.courentView addSubview:self.datePicker];
    
    //add tool bar
    UIView *buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kBCSCREEN_WIDTH, kHHDatePickerView_PickBarH)];
    buttonView.backgroundColor = [UIColor bc_colorWithRGB:0xF4F4F4];
    [self.courentView addSubview:buttonView];
 
    [buttonView addSubview:self.cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        make.width.mas_equalTo(60);
    }];
    
    [buttonView addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        make.width.mas_equalTo(60);
    }];
    //add split line
    [self.courentView addSubview:self.splitLb];
}

//MARK: - getter
-(UIView *)courentView {
    if (!_courentView) {
        _courentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bc_bottom, kBCSCREEN_WIDTH, kHHDatePickerView_PickH+kHHDatePickerView_PickBarH+kBCScreenSafeAreaBottom)];
        _courentView.backgroundColor = [UIColor whiteColor];
    }
    return _courentView;
}
-(UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        if (@available(iOS 13.4, *)) {
            _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
        _datePicker.frame = CGRectMake(0, kHHDatePickerView_PickBarH, kBCSCREEN_WIDTH, kHHDatePickerView_PickH);
        [_datePicker setDatePickerMode:UIDatePickerModeDate];
        [_datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}
-(UIView *)splitLb {
    if (!_splitLb) {
        _splitLb = [[UIView alloc] initWithFrame:CGRectMake(0, kHHDatePickerView_PickBarH-0.5, self.bc_width, 0.5)];
        _splitLb.backgroundColor = BCComConfig.config.splitColor;
    }
    return _splitLb;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _cancelButton.titleLabel.font = BCComConfig.config.bcFont(15);
        [_cancelButton setTitle:BCLOC(@"取消") forState:UIControlStateNormal];
        [_cancelButton setTitleColor:BCComConfig.config.blackColor forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancelBarItemAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}
- (UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _doneButton.titleLabel.font = BCComConfig.config.bcFont(15);
        [_doneButton setTitle:BCLOC(@"确定") forState:UIControlStateNormal];
        [_doneButton setTitleColor:BCComConfig.config.defaultColor forState:UIControlStateNormal];
        [_doneButton addTarget:self action:@selector(doneBarItemAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneButton;
}
@end
